/*
 * @Author: wanchunyi 1536793586@qq.com
 * @Date: 2022-11-21 16:06:59
 * @LastEditors: wanchunyi 1536793586@qq.com
 * @LastEditTime: 2023-02-22 17:37:15
 * @FilePath: \virtualHome\src\utils\xiaz.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 下载图片
 *
 * @param {*} path 下载地址/下载请求地址。
 * @param {string} [name='name.jpg'] 下载文件的名字（考虑到兼容性问题，最好加上后缀名
 */
export const downloadFile = (path, name = 'name.jpg') => {
    const download = (href) => {
        const a = document.createElement('a')
        a.style.display = 'none'
        a.href = href
        a.download = name
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
    }

    if (isIE()) {
        download(path)
    } else {
        const xhr = new XMLHttpRequest()
        xhr.open('get', path)
        xhr.responseType = 'blob'
        xhr.send()
        xhr.onload = (e) => {
            const { status, response } = e.target
            if (status === 200 || status === 304) {
                const fileReader = new FileReader()
                fileReader.readAsDataURL(response)
                fileReader.onload = (ev) => {
                    download(ev.target.result)
                }
            }
        }
    }
}
// 判断是否IE
export function isIE() {
    return /msie|trident/.test(window.navigator.userAgent.toLowerCase())
}

//下载视频

/**
 * 
 * @param {String} path //路径
 * @param {String} name //名字
 */

export function mp4xiaz(path, name) {
    fetch(path).then(res => res.blob()).then(blob => {
        const a = document.createElement('a');
        document.body.appendChild(a)
        a.style.display = 'none'
        const url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = name + '.mp4';
        a.click();
        document.body.removeChild(a)
        window.URL.revokeObjectURL(url);
    });
}